#include <iostream>
using namespace std;

class Node
{
	public:
		Node *next;
		int data;
};

class CircleList
{
	private:
    	Node* head;
    	int length;
    public:
    	CircleList()
    	{
    		head = new Node();
    		head->next = head;
    		head->data = 0;
    		length = 0;
		}
    	~CircleList()
    	{
    		delete(head);
		}
    	void createCircleList(int n);
    	void traverseNode();
		void deleteNode(int n);
		void insertNode(int n,int data);
		int getLength();
		bool isEmpty();
};
void CircleList::createCircleList(int n)
{
	if(n<0)
	{
		cout << "你输入的长度不正确 " << endl;
	}
	else
	{
		length = n;
		printf("请输入你要存储的数据:");
        Node *p,*q;
        p = head;
        while(n--)
        {
        	q = new Node();
			cin >> q->data;
			p->next = q;
			q->next = head;
			p = q;
		}
	}
}
void CircleList::traverseNode()
{
	Node *p;
	p = head->next;
	while(p!=head)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

void CircleList::deleteNode(int n)
{
	if(n<0||n>length)
	{
		printf("删除的位置是错误的\n");
		return;
	}
	else
	{
		Node *p,*q;
		p = head;
		for(int i=1;i<n;i++)
			p=p->next;
		q = p->next;
		p->next = q->next;
		delete q;
		q = NULL;
        length--;
	}
}

void CircleList::insertNode(int n,int data)
{
	Node *q,*p = new Node();
	p->data = data;
	q = head;
	for(int i = 1;i<n;i++)
		q = q->next;
	p->next = q->next;
	q->next = p;
}


int CircleList::getLength()
{
	return length;
}

bool CircleList::isEmpty()
{
	return head==head->next;
}

int main()
{
	CircleList list;
	int n,data;
	cout << "请输入你要存储的数据个数:";
	cin >> n;
	list.createCircleList(n);
	list.traverseNode();

	cout << "请选择你要删除的结点位置:";
	cin >> n;
	list.deleteNode(n);
	list.traverseNode();

	cout << "请输入你要插入的下标和值:";
	cin >> n >> data;
	list.insertNode(n , data);
	list.traverseNode();

	return 0;
}
